<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>上传照片测试</title>
</head>

<body>
    <h1>上传照片测试</h1>
    <input type="file" id="fileInput" />
    <button onclick="uploadPhoto()">上传照片</button>

    <script>
        function uploadPhoto() {
            const fileInput = document.getElementById('fileInput');
            const file = fileInput.files[0];
            const id = 1; // 假设要上传照片的教师ID为1

            if (!file) {
                alert('请选择文件');
                return;
            }

            const formData = new FormData();
            formData.append('file', file);
            formData.append('id', id);

            const xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://localhost:8080/teacher/uploadphoto', true);

            xhr.onload = function () {
                if (xhr.status === 200) {
                    console.log('上传成功:', xhr.responseText);
                    alert('上传成功');
                } else {
                    console.error('上传失败:', xhr.statusText);
                    alert('上传失败');
                }
            };

            xhr.onerror = function () {
                console.error('请求失败');
                alert('请求失败');
            };

            xhr.send(formData);
        }
    </script>
</body>

</html>